package spark.network.strategy;

import org.apache.log4j.Logger;
import spark.network.ITimeAdjuster;
import spark.network.Sender;
import spark.network.protocol.Acknowledgement;
import spark.network.protocol.Base;
import spark.network.protocol.ClockAdjustNotice;
import spark.network.protocol.Response;
import spark.utility.OutWrapper;

import java.util.Collection;

public class ClockAdjustNoticeStrategy implements IProtocolHandleStrategy<ClockAdjustNotice> {
	private static Logger logger = Logger.getLogger(ClockAdjustNoticeStrategy.class);

	private ITimeAdjuster timeAdjuster;

	public ClockAdjustNoticeStrategy(ITimeAdjuster timeAdjuster) {
		this.timeAdjuster = timeAdjuster;
	}

	@Override
	public void handle(ClockAdjustNotice packet, OutWrapper<Response> acknowledgement, Collection<Base> responses) {
		logger.info("Adjusting time by " + packet.getAdjustment());
		timeAdjuster.setTimeAdjustment(packet.getAdjustment());

		acknowledgement.value = new Acknowledgement(packet);
	}
}